package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"
	"github.com/kataras/iris/v12"
)

type EnrollProfile struct {
	Ctx iris.Context
}

// 获取大学招生简章列表
func (c *EnrollProfile) PostList() interface{} {
	id := c.Ctx.PostValueDefault("id", "0")

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	enrollProfileList := make([]models.EnrollProfileObj, 0)
	err = db.Table(new(models.EnrollProfile)).Alias("ep").
		Join("INNER", []string{"university", "u"}, "ep.university_id = u.id").
		Where("ep.university_id = ?", id).Find(&enrollProfileList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取招生简章列表：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取招生简章列表", enrollProfileList)
}

// 获取招生简章详情
func (c *EnrollProfile) PostDetail() interface{} {
	id := c.Ctx.PostValueDefault("id", "0")

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	enrollProfile := new(models.EnrollProfile)
	_, err = db.ID(id).Get(enrollProfile)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取招生简章详情：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取招生简章详情", enrollProfile)
}